
import axios from "axios";
import notify from "./notify";
// import Vue from "vue";
// import { URL_MAP } from "../config/config";
// import { URL_MAP } from "./config";
// import { getDevData, getPersonInfoList } from "./dev-data"
/* eslint-disable no-unreachable */
export const URL_MAP = {
    LOGIN: 'api-token/',
    ZXJC: 'api/zxjc/',
    DCC: 'api/dcc/',
}

export async function getData(){
    let res = await axios.get('data/data1.json')
    return res.data
}
export const backendUrl = '/backend/'
// const geoServerUrl = '/geoserver/'

// axios.get('/backend/xx')

export const instance = axios.create({
    baseURL: backendUrl
})

export const fileDownload = axios.create({
    baseURL: ''
})


export async function login(username, passwd) {
    console.log(username, passwd)
    return {
        token: 'xxxxxx',
    }
    let response = await instance.post(URL_MAP.LOGIN, {
        'username': username,
        'password': passwd,
        // 'uuid': uuid,
        // 'captch': capthe
    })

    let data = await response.data
    console.log(data)
    // // data.phoneNumber = username
    // // data.username = username
    let token = data.token
    setAuth(token);
    // // response = await instance.get('current/')
    // // data = await response.data
    // // data.token = token
    return data
}

export function setAuth(auth) {
    instance.defaults.headers['Authorization'] = 'JWT ' + auth;
}

export async function logout() {
    sessionStorage.clear();

    instance.defaults.headers['Authorization'] = ''
}


export async function get_all_data(name = 'all_data.json') {
    // return {'code':0}
    try {
        let resp = await axios.get('data/' + name)
        if (resp.status !== 200) {
            notify.notify(`${resp.status}:${resp.statusText}`)
            return { 'code': -1, msg: `${resp.status}:${resp.statusText}` }
        }
        return resp.data
    }
    catch (e) {
        return {
            'code': -2,
            'msg': e
        }
    }
}

export async function get_data(url, params) {
    // return {'code':0}
    try {
        let resp = await instance.get(url, {
            params: params,
        })
        if (resp.status !== 200) {
            notify.notify(`${resp.status}:${resp.statusText}`)
            return { 'code': -1, msg: `${resp.status}:${resp.statusText}` }
        }
        return resp.data
    }
    catch (e) {
        return {
            'code': -2,
            'msg': e
        }
    }
}